package com.example.activemq.queue;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class Consumer {

    public static final String CONNECT_URL = "tcp://localhost:61616";

    public static void main(String[] args) throws Exception {
        ActiveMQConnectionFactory mqConnectionFactory = new ActiveMQConnectionFactory(CONNECT_URL);

        Connection activeMQConnection = mqConnectionFactory.createConnection();

        activeMQConnection.start();

        Session session = activeMQConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        Destination queue = session.createQueue("test");

        MessageConsumer messageConsumer = session.createConsumer(queue);

        while (true) {
            TextMessage message = (TextMessage) messageConsumer.receive();
            if (message != null) {
                System.out.println("消费者接收到消息：" + message);
            }
            break;
        }
        messageConsumer.close();
        session.close();
        activeMQConnection.close();
    }
}
